package ro.ubbcluj.cs.damate.managers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import ro.ubbcluj.cs.damate.persistence.entities.User;
import ro.ubbcluj.cs.damate.services.UserService;
import ro.ubbcluj.cs.damate.webutils.MessageProvider;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.ValidatorException;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * @author Lorand Fazakas
 */
public class BaseView {

    @Autowired
    private UserService userService;

    public void validateUsername(FacesContext context, UIComponent component, Object value) {
        if (value.toString().trim().length() < 4 || !Pattern.compile("[a-zA-Z_0-9-.]+").matcher(value.toString().trim()).matches()) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, MessageProvider.getValue("userManagement.inputFields.validationMessages.username.format"), null);
            throw new ValidatorException(message);
        } else {
            User user = userService.findByUsername(value.toString().trim());
            if (user != null) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, MessageProvider.getValue("userManagement.inputFields.validationMessages.username.exist"), null);
                throw new ValidatorException(message);
            }
        }
    }

    public void validatePassword(FacesContext context, UIComponent component, Object value) {
        if (value.toString().trim().length() < 8 || !Pattern.compile("[a-zA-Z_0-9-.]+").matcher(value.toString().trim()).matches()) {
            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, MessageProvider.getValue("userManagement.inputFields.validationMessages.password.format"), null);
            throw new ValidatorException(message);
        } else {
            String password = getPasswordParameter(context.getExternalContext().getRequestParameterMap(), "id-password");
            if (password != null && !value.equals(password)) {
                FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_ERROR, MessageProvider.getValue("userManagement.inputFields.validationMessages.passwords.notEquals"), null);
                throw new ValidatorException(message);
            }
        }
    }

    private String getPasswordParameter(Map<String, String> requestParams, String paramName) {
        for(Map.Entry<String, String> param : requestParams.entrySet()) {
            if (param.getKey().endsWith(paramName)) {
                return param.getValue();
            }
        }
        return null;
    }

    public String getLoggedInUser() {
        return ((org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUsername();
    }
}
